export default {
  data() {
    return {
      module   : 'users',
      data     : [],
      total    : 0,
      page     : 1,
      docs     : 5,
      modalForm: false,
      ids      : [],
      searchBox: "",
    };
  },
  methods: {
    handleSubmit(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          if (this.formValidate._id) {
            this.axios({
              url: `http://10.31.162.59:3000/${this.module}/${
                this.formValidate._id
              }`,
              method: 'put',
              data  : this.formValidate
            }).then(res => {
              this.modalForm = false;
              this.getData();
              this.$data.formValidate = this.$options.data().formValidate;
            });
          } else {
            this.axios({
              url   : `http://10.31.162.59:3000/${this.module}`,
              method: 'post',
              data  : this.formValidate
            }).then(res => {
              this.modalForm = false;
              this.getData();
            });
          }
        } else {
          this.$Message.error('Fail!');
        }
      });
    },
    handleReset(name) {
      this.$refs[name].resetFields();
    },
    show(id){
      this.$Modal.info({
        title  : '活动详情',
        content: `主题：${this.data[id].theme}<br>标题：${this.data[id].title}<br>内容：${this.data[id].content}<br>活动时间：${this.data[id].time}<br>报名开始时间：${this.data[id].startTime}<br>报名截止时间：${this.data[id].endTime}<br>主办方：${this.data[id].main}<br>协办方：${this.data[id].second}`
    })
    },
    edit(id) {
      // 请示数据，打开对话框，显示表单的数据，进行提交
      this.axios({
        url   : `http://10.31.162.59:3000/${this.module}/${id}`,
        method: 'put'
      }).then(res => {
        this.formValidate = res.data;
        this.modalForm    = true;
      });
    },
    remove(id) {
      this.$Modal.confirm({
        title  : '确认操作',
        content: '<p>你确认删除该记录吗?</p>',
        onOk   : () => {
          this.axios({
            url   : `http://10.31.162.59:3000/${this.module}/${id}`,
            method: 'delete'
          }).then(res => {
            this.getData();
          });
        },
        onCancel: () => {
          this.$Message.info('Clicked cancel');
        }
      });
    },
    getData() {
      this.axios({
        url   : `http://10.31.162.59:3000/${this.module}/list`,
        method: 'post',
        data  : {
          page: this.page,
          docs: this.docs
        }
      }).then(res => {
        this.total = res.data.total;
        this.data  = res.data.docs;
      });
    },
    // onChangePage(page) {
    //   this.page = page;
    //   this.getData();
    // },
    // onPageSizeChange(rows) {
    //   this.rows = rows;
    //   this.getData();
    // },
    onSelect(selections) {
      var ids = [];
      for (let i = 0; i < selections.length; i++) {
        ids.push(selections[i]._id);
      }
      this.ids = ids.toString();
    },
    removes() {
      console.log(this.ids)
      this.$Modal.confirm({
        title  : '确认操作',
        content: '<p>你确认删除该记录吗?</p>',
        onOk   : () => {
          this.axios({
            url   : `http://10.31.162.59:3000/${this.module}`,
            method: 'delete',
            data  : {
              ids: this.ids
            }
          }).then(res => {
            this.getData();
          });
        },
        onCancel: () => {
          this.$Message.info('Clicked cancel');
        }
      });
    },

    onSearch() {
      this.axios({
        url   : `http://10.31.162.59:3000/${this.module}/list`,
        method: 'post',
        data  : {
          title: this.searchBox,
          page : this.page,
          docs : this.docs
        }
      }).then(res => {
        this.total = res.data.total;
        this.data  = res.data.docs;
      });
    },

    onChangePage(page) {
      this.page = page;
      if (this.searchBox) {
        this.onSearch();
      } else {
        this.getData();
      }
    },
    onPageSizeChange(docs) {
      this.docs = docs;
      if (this.searchBox) {
        this.onSearch();
      } else {
        this.getData();
      }
    },



  },
  mounted() {
    this.getData();
  }
};
